package com.atguigu.tingshu.user.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.common.rabbit.service.RabbitService;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

	@Autowired
	private UserInfoMapper userInfoMapper;
	@Autowired
	private WxMaService wxMaService;
	@Autowired
	private RabbitService rabbitService;
	@Autowired
	private RedisTemplate redisTemplate;

	@Override
	public Map<String, Object> wxLogin(String code) {
//		//创建map集合
//		Map<String, Object> map = new HashMap<>();
//		WxMaJscode2SessionResult wxMaJscode2SessionResult = null;
//		try {
//			wxMaJscode2SessionResult = wxMaService.jsCode2SessionInfo(code);
//		} catch (WxErrorException e) {
//			throw new RuntimeException(e);
//		}
//		String openid = wxMaJscode2SessionResult.getOpenid();
//		//有openid(微信唯一标识)去查询用户,没有直接去注册
//		UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getWxOpenId, openid));
//		if (userInfo == null) {
//			userInfo = new UserInfo();
//			//赋值
//			userInfo.setWxOpenId(openid);
//			userInfo.setNickname("沙雕听友" + System.currentTimeMillis());
//			userInfo.setAvatarUrl("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
//			this.userInfoMapper.insert(userInfo);
//			//给新用户冲点小钱
//			rabbitService.sendMessage(MqConst.EXCHANGE_USER, MqConst.ROUTING_USER_REGISTER, userInfo.getId());
//		}
//			//创建token
//			String token = UUID.randomUUID().toString().replaceAll("-", "");
//			//存储缓存
//			String loginKey = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
//			this.redisTemplate.opsForValue().set(loginKey, userInfo, RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
//			//存储touken
//			map.put("token", token);
//			return map;
		//	创建map 集合
		Map<String, Object> map = new HashMap<>();
		//
		WxMaJscode2SessionResult wxMaJscode2SessionResult = null;
		try {
			wxMaJscode2SessionResult = wxMaService.jsCode2SessionInfo(code);
		} catch (WxErrorException e) {
			throw new RuntimeException(e);
		}
		String openid = wxMaJscode2SessionResult.getOpenid();
		//	openid 是每个微信账号唯一标识; 利用openid 查询用户信息;如果用户信息表中有数据，则已经注册；如果没有未注册，直接注册.
		UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getWxOpenId, openid));
		if (userInfo == null) {
			//	没有注册;
			userInfo = new UserInfo();
			//	赋值：
			userInfo.setWxOpenId(openid);
			userInfo.setNickname("听友:" +System.currentTimeMillis());
			userInfo.setAvatarUrl("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
			this.userInfoMapper.insert(userInfo);
			//	为了留住用户；需要给新注册的用户一笔资金 1000; 发送的内容是什么? 由消费者决定！
			rabbitService.sendMessage(MqConst.EXCHANGE_USER,MqConst.ROUTING_USER_REGISTER,userInfo.getId());
		}
		//	创建token；
		String token = UUID.randomUUID().toString().replaceAll("-", "");
		//	将用户信息存储到缓存;
		String loginKey = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
		this.redisTemplate.opsForValue().set(loginKey,userInfo,RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
		//	存储token
		map.put("token",token);
		//	返回数据
		return map;

		}

	@Override
	public UserInfoVo getUserInfo(Long userId) {
		//c创建用户对象
		UserInfoVo userInfoVo = new UserInfoVo();
		//查询用户信息
		UserInfo userInfo = this.getById(userId);
		//拷贝
		BeanUtils.copyProperties(userInfo, userInfoVo);
		return userInfoVo;
	}

	@Override
	public void updateUser(Long userId, UserInfoVo userInfoVo) {
		//创建对象
		UserInfo userInfo = new UserInfo();
		//拷贝
		BeanUtils.copyProperties(userInfoVo, userInfo);
		userInfo.setId(userId);
		//更新
		this.updateById(userInfo);

	}
}


